package com.wh.wisdomsite.xbox.server.util;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wh.wisdomsite.xbox.ba.entity.Role;
import com.wh.wisdomsite.xbox.common.Constants;
import com.wh.wisdomsite.xbox.server.session.ServerSessionUser;
import com.wh.wisdomsite.xbox.us.entity.Company;
import com.wh.wisdomsite.xbox.us.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户工具类
 *
 * @version V1.0
 * @Package: com.wh.wisdomsite.xbox.server.util
 * @author: 丁奕
 * @date: 2018-08-21 14:57
 * @Copyright 公司版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2016
 **/
public class UserUtils {

    public static ServerSessionUser getServerSessionUser(){
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        return (ServerSessionUser) request.getSession(true).getAttribute(Constants.SERVER_SESSION_USER);
    }

    /**
     * 获取当前所登陆的用户
     * @return
     */
    public static User getCurrentUser(){
        User user = getServerSessionUser().getUser();
        return user;
    }

    /**
     * 获取当前所登陆的用户的总公司
     * @return
     */
    public static Company getCurrentUserParentCompany(){
        Company company = getServerSessionUser().getHeadCompany();
        return company;
    }

    /**
     * 当前登录用户是否超级管理员或者监管管理员
     * @return
     */
    public static boolean isSysAdmin(){
        for(Role role : getServerSessionUser().getRoleList()){
            if(role != null && (role.getRoId().equals("0") || role.getRoId().equals("2"))){
                return true;
            }
        }
        return false;
    }

    /**
     * 当前登录用户是否公司管理员
     * @return
     */
    public static boolean isCompAdmin(){
        for(Role role : getServerSessionUser().getRoleList()){
            if(role != null && role.getRoId().equals("1")){
                return true;
            }
        }
        return false;
    }

    /**
     * 在用户不是超级管理员的时候根据用户的数据权限设置查找范围
     * @param ew
     * @param companyTableAlias
     * @param officeTableAlias
     * @param projectTableAlias
     * @return
     */
    public static EntityWrapper setEntityWrapper(EntityWrapper ew, String companyTableAlias,
               String officeTableAlias, String projectTableAlias) {
        ServerSessionUser serverSessionUser = getServerSessionUser();
        String dataScope = serverSessionUser.getDataScope();
        ew = swichDataScope(ew, dataScope, serverSessionUser, companyTableAlias, officeTableAlias, projectTableAlias);
        return ew;
    }

    /**
     * 根据用户的数据权限设置查找范围
     * @param ew
     * @param serverSessionUser
     * @param companyTableAlias
     * @param officeTableAlias
     * @param projectTableAlias
     * @return
     */
    public static EntityWrapper setEntityWrapper(EntityWrapper ew, ServerSessionUser serverSessionUser, String companyTableAlias,
                                                 String officeTableAlias, String projectTableAlias) {
        String dataScope = serverSessionUser.getDataScope();
        ew = swichDataScope(ew, dataScope, serverSessionUser, companyTableAlias, officeTableAlias, projectTableAlias);
        return ew;
    }

    public static EntityWrapper swichDataScope(EntityWrapper ew, String dataScope, ServerSessionUser serverSessionUser,
                                               String companyTableAlias, String officeTableAlias, String projectTableAlias){
        //设置一个查询用的代码
        String codeForFind = null;
        switch (dataScope) {
            case "1":
                codeForFind = serverSessionUser.getFinalCompCode();
                if(StringUtils.isNotEmpty(codeForFind)){
                    ew.like(companyTableAlias + ".compCode", codeForFind);
                }
                break;
            case "2":
                codeForFind = serverSessionUser.getFinalCompCode();
                if(StringUtils.isNotEmpty(codeForFind)){
                    ew.eq(companyTableAlias + ".compCode", codeForFind);
                }
                break;
            case "3":
                codeForFind = serverSessionUser.getEmployee().getOffice().getOfficeCode();
                if(StringUtils.isNotEmpty(codeForFind)){
                    ew.like(officeTableAlias +".officeCode", codeForFind);
                }
                break;
            case "4":
                codeForFind = serverSessionUser.getEmployee().getOffice().getOfficeCode();
                if(StringUtils.isNotEmpty(codeForFind)){
                    ew.eq(officeTableAlias + ".officeCode", codeForFind);
                }
                break;
            case "5":
                codeForFind = serverSessionUser.getEmployee().getPrFinished() + "," + serverSessionUser.getEmployee().getPrId();
                if(StringUtils.isNotEmpty(codeForFind)){
                    ew.in(projectTableAlias + ".prId", codeForFind);
                }
                break;
            default:
                break;
        }
        return ew;
    }
}
